﻿//   SystemExtensions.cs
//   Copyright (c) 2012 Eswar Prakash
// 
//    This library is free software; you can redistribute it and/or modify  
//    it  under the terms of the GNU Lesser General Public License version  
//    2.1 as published by the Free Software Foundation.                     
//                                                                          
//    This library is distributed in the hope that it will be useful, but   
//    WITHOUT ANY WARRANTY; without even the implied warranty of            
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     
//    Lesser General Public License for more details.                       
//                                                                          
//    You should have received a copy of the GNU Lesser General Public      
//    License along with this library; if not, write to the Free Software   
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
//    USA                                                                 
// 
namespace CKSAS.Core.Utilities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Extensions for system objects like string, int etc.
    /// </summary>
    public static class SystemExtensions
    {
        /// <summary>
        /// RegEx pattern for splitting enumerations to strings
        /// </summary>
        private const string SplitPattern = @"(?-i:[A-Z]{1}[a-z0-9]+)";

        /// <summary>
        /// Returns the value of a string split on the upper case letters to form a title
        /// </summary>
        /// <param name="s">The string for which the title case needs to be returned.</param>
        /// <returns></returns>
        public static string AsTitle(this string s)
        {
            // split and phrase using sentence case
            var captures = Regex.Matches(s, SplitPattern)
                .OfType<Match>()
                .Where(x => x.Success)
                .Select(x => x.Value)
                .ToArray();

            return String.Join(" ", captures);
        }
    }
}